<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: Lucky
    // +----------------------------------------------------------------------
    // | Date: 2020/3/25-15:59
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\admin\controller\queue;

    use app\common\controller\AdminBaseController;
    use Cron\CronExpression;
    use think\Exception;
    use think\facade\Db;

    class Queue extends AdminBaseController
    {
        public function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub

            $this->model = new \app\admin\model\Queues();

            $this->assign("typelist", $this->model->getTypeList());
        }

        /**
         * @param \app\admin\model\Queues $queues
         * @return string
         * @throws \Exception
         * @author: Lucky
         * @date: 2020/7/30 0030
         * @describe:
         */
        public function addQueue(\app\admin\model\Queues $queues)
        {

            if (IS_POST) {
                $data = $this->request->post();

                $sd = $this->check_schedule($data['schedule']);
                if (!$sd) {
                    $this->error("执行周期格式错误");
                }
                    $k = $queues->where("title", $data['title'])->find();
                    if ($k) {
                        $this->error("当前定时任务名称已存在");
                    }
                return $this->model->doAll($data);
            }

            $this->assign("info", []);

            return $this->fetch();
        }


        /**
         * @return string
         * @throws \Exception
         * @author: Lucky
         * @date: 2020/7/30 0030
         * @describe:
         */
        public function editQueue()
        {
            $id   = $this->request->param("id/d");
            $info = $this->model->getOne($id);

            if (!$info) {
                $this->assign('msg', '当前id:'.$id.'错误');
                $this->fetch("public/404");
            }

            $this->assign("info", $info);

            return $this->fetch("add_queue");
        }


        /**
         * @return string
         * @throws \Exception
         * @author: Lucky
         * @date: 2020/7/30 0030
         * @describe:
         */
        public function viewQueueLog()
        {

            $id  = $this->request->param("id/d");
            $res = Db::name("sys_queue_log")->where("crontab_id", $id)->order("id desc")->paginate(15);

            $this->assign('data', $res->toArray());

            $this->assign('page', $res->render());

            return $this->fetch("queue_log");
        }


        /**
         * @throws \think\db\exception\DbException
         * @author: Lucky
         * @date: 2020/7/30 0030
         * @describe:
         */
        public function logDel()
        {
            if (IS_POST) {
                $id = $this->request->post("id");
                try {
                    $res = Db::name("sys_queue_log")->where("id", $id)->delete();
                    $res ? $this->success("删除成功") : $this->error("删除失败");
                } catch (Exception $exception) {
                    $this->error($exception->getMessage());
                }

            }
        }


        /**
         * @param $schedule
         * @return bool
         * @author: Lucky
         * @date: 2020/3/25
         * @name: check_schedule
         * @describe:判断Crontab格式是否正确
         */
        protected function check_schedule($schedule)
        {
            if (CronExpression::isValidExpression($schedule)) {
                return true;
            } else {
                return false;
            }
        }



        /**
         * @author: Lucky
         * @date: 2020/3/25
         * @name: get_schedule_future
         * @describe:根据Crontab表达式读取未来七次的时间
         */
        public function get_schedule_future()
        {
            $time     = [];
            $schedule = $this->request->post('schedule');
            $days     = (int)$this->request->post('days');
            try {
                $cron = CronExpression::factory($schedule);
                for ($i = 0; $i < $days; $i++) {
                    $time[] = $cron->getNextRunDate(null, $i)->format('Y-m-d H:i:s');
                }
            } catch (\Exception $e) {

            }
            $this->success("", null, $time);
        }



    }